-- #############################################################################
-- mpi-tests3.lua
-- 
-- by Jacob Smith <http://code.google.com/p/lua-mpi/>
-- Copyright (C) 2009 Jacob Smith, All Rights Reserved
-- 
-- MPI extra tests.
-- 
-- $Id: mpi-tests3.lua 15 2009-07-16 09:24:06Z kloptops $

-- #############################################################################
-- Import stuff we require.
require('mpi');

function mpi_fib(n)
    local f1 = {1, sign='+'};
    local f2 = {0, sign='+'};
    local fib = mpi_new();

    if n < 2 then
        mpi_set(fib, n);
        return fib;
    end

    for i=2,n do
        mpi_add(f1, f2, fib);
        mpi_copy(f2, f1);
        mpi_copy(f1, fib);
    end

    return fib;
end

function timeit(s, f, n)
	local c=os.clock()
	local v=f(n)
	local t=os.clock()-c
	print(s, n, t);
	print(mpi_bitcount(v), mpi_tostring(v, 16));
end

timeit("fib", mpi_fib, 64);
timeit("fib", mpi_fib, 128);
timeit("fib", mpi_fib, 512);
